package machine;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Guilherme
 */
public class MongoDB {

    private Mongo bdConnection;
    private DB db;

    public MongoDB() {
    }

    public boolean connect() {
        try {
            System.out.println("\nConectando com banco de dados...");
            this.bdConnection = new Mongo("localhost", 27017);
            this.db = this.bdConnection.getDB("gadabout");
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public int getDocumentLenght() {
        DBCollection collection = this.db.getCollection("documentList");
        return Integer.parseInt(collection.getCount() + "");
    }

    public int getDocumentLenght(Long documentID) {
        DBCollection collection = this.db.getCollection("documentList");
        BasicDBObject search = new BasicDBObject();
        search.put("_id", Integer.parseInt(documentID.toString()));
        DBCursor cursor = collection.find(search);
        DBObject rtr = cursor.next();
        return Integer.parseInt(rtr.get("lenght").toString());
    }

    public double getAvgDocument() {
        DBCollection collection = this.db.getCollection("documentList");
        int nDocuments = Integer.parseInt(collection.getCount() + "");
        double avg = 0;
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            avg += Integer.parseInt(cursor.next().get("lenght") + "");
        }
        return (avg / nDocuments);
    }

    public List<Page> fillList(String string) {
        ArrayList listDocument = new ArrayList();

        DBCollection collection = this.db.getCollection("vocabularyList");
        DBCursor cursor = collection.find();

        int idToken = -1;
        if (cursor.hasNext()) {
            idToken = Integer.parseInt(cursor.next().get("_id") + "");
        }

        if (idToken != -1) {
            BasicDBObject search = new BasicDBObject();
            search.put("_id", idToken);
            collection = this.db.getCollection("vocabularyList");
            cursor = collection.find(search);
            while (cursor.hasNext()) {
                //Integer.parseInt(cursor.next().get("lenght") + "")
                listDocument.add(new Page());
            }
        }
        return null;
    }

    public Long getTokenID(String token) {
        DBCollection collection = this.db.getCollection("vocabularyList");
        BasicDBObject search = new BasicDBObject();
        search.put("token", token);
        DBCursor cursor = collection.find(search);
        if (cursor.hasNext()) {
            DBObject result = cursor.next();
            return Long.parseLong(result.get("_id").toString());
        } else {
            return (long) -1;
        }
    }

    public HashMap<Long, Integer> getTokenProperties(Long tokenID) {
        DBCollection collection = this.db.getCollection("invertedList");
        BasicDBObject search = new BasicDBObject();
        search.put("vocabularyId", tokenID);
        DBCursor cursor = collection.find(search);
        HashMap<Long, Integer> rs = new HashMap<Long, Integer>();
        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            Integer frequency = Integer.parseInt(result.get("tokenFrequency").toString());
            Long documentID = Long.parseLong(result.get("documentId").toString());
            rs.put(documentID, frequency);
        }
        return rs;
    }

    boolean orderByDocumentRank(long doc1, long doc2) {
        DBCollection collection = this.db.getCollection("documentList");

        BasicDBObject search1 = new BasicDBObject();
        search1.put("_id", doc1);
        DBCursor cursor1 = collection.find(search1);
        DBObject obj1 = cursor1.next();

        BasicDBObject search2 = new BasicDBObject();
        search2.put("_id", doc2);
        DBCursor cursor2 = collection.find(search2);
        DBObject obj2 = cursor2.next();
        return Double.parseDouble(obj1.get("rank").toString()) > Double.parseDouble(obj2.get("rank").toString());
    }
    
    public Page getPage( Long documentID ){
        DBCollection collection = this.db.getCollection("documentList");
        BasicDBObject search1 = new BasicDBObject();
        search1.put("_id", documentID);
        DBCursor cursor1 = collection.find(search1);
        DBObject obj1 = cursor1.next();
        return new Page( obj1.get("path").toString(), obj1.get("title").toString() );
    }

}
